#include"SeqStack.h"

void Init_SeqStack(SeqStack* pss)
{
	assert(pss);

	pss->arr = (ELEMTYPE*)malloc(sizeof(ELEMTYPE) * InitSize);

	if (!pss->arr)
		exit(EXIT_FAILURE);

	pss->top = 0;
	pss->StackSize = 0;

}

bool Push(SeqStack* pss, ELEMTYPE val)
{
	assert(pss);

	if (Full(pss))
		Increase(pss);

	pss->arr[pss->top++] = val;

	return true;
}

bool Pop(SeqStack* pss)
{
	assert(pss);

	if (Empty(pss))
		return false;

	pss->top--;

	return true;
}

ELEMTYPE Top(SeqStack* pss)
{
	assert(pss);

	if (Empty(pss))
		exit(EXIT_FAILURE);

	return pss->arr[pss->top - 1];
}

int Size(SeqStack* pss)
{
	assert(pss);
	return pss->top;
}

bool Empty(SeqStack* pss)
{
	assert(pss);
	return pss->top == 0;
}

bool Full(SeqStack* pss)
{
	assert(pss);
	return pss->top == pss->StackSize;
}

void Increase(SeqStack* pss)
{
	assert(pss);

	ELEMTYPE* tmp = (ELEMTYPE*)realloc(pss->arr, sizeof(ELEMTYPE) * pss->StackSize * 2);
	if (!tmp)
		exit(EXIT_FAILURE);

	pss->arr = tmp;
	pss->StackSize *= 2;
}

void Destroy(SeqStack* pss)
{
	assert(pss);
	free(pss->arr);
	pss->arr = NULL;
	pss->top = 0;
	pss->StackSize = InitSize;
}

void Show(SeqStack* pss)
{
	assert(pss);

	for (int i = pss->top - 1; i >= 0; i--)
		printf("%d -> ", pss->arr[i]);
	printf("NULL\n");
}

int main()
{
	SeqStack head;

	Init_SeqStack(&head);
	printf("%d\n", head.StackSize);

	Push(&head, 11);
	Push(&head, 10);
	Push(&head, 9);
	Push(&head, 8);
	Push(&head, 7);
	Push(&head, 6);
	Push(&head, 5);
	Push(&head, 4);
	Push(&head, 3);
	Push(&head, 2);
	Push(&head, 1);
	Show(&head);

	printf("%d\n", head.StackSize);

	Pop(&head);
	Show(&head);

	printf("%d\n", Top(&head));

	printf("%d\n", Size(&head));

	Destroy(&head);

	printf("%d\n", head.top);

	return 0;
}